

require('./common');

exports.load = function(req, res){
	
	var tableID = req.params.tableID;
	var requestID = req.params.requestID;
	var comment = req.params.comment;
	
	if(tableID=="0" && requestID=="0"){
		var strSQL = "SELECT * FROM tblcomment ORDER BY ID Desc";	
	}else{
		var strSQL = "SELECT * FROM tblcomment WHERE TableID=" + conn.escape(tableID) + " AND RequestID = " + conn.escape(requestID) + " ORDER BY ID Desc";	
	}
	
	//var strSQL = "CALL sp_getComments(" + conn.escape(tableID) + "," + conn.escape(requestID) + ")"
	addComment(tableID, requestID, comment)
	conn.query(strSQL, function(err, results, fields){
		if(err) throw err;
		res.send(results);
	});
}

function addComment(tableID, requestID, comment){
		var strSQL = "INSERT INTO tblcomment (TableID, RequestID, Content, DateCreated, DateUpdated, CreatedBy, UpdatedBy) VALUES (" + conn.escape(tableID) + ", " + conn.escape(requestID) + ", " + conn.escape(comment) + ",Now(),Now(),'Stephen Curry','Stephen Curry')"
		
		if(tableID != "undefined" && requestID != "undefined" && comment!= "undefined"){
			conn.query(strSQL);
			console.log("done adding comment...")
		}
}
